package linkedList.递归.剑指;

import linkedList.BaseLinkedListTest;
import po.ListNode;

import java.util.Stack;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 剑指offer从尾到头打印链表06.java
 * @Description TODO
 * @createTime 2021年03月20日 11:56:00
 */
public class 剑指offer从尾到头打印链表06 extends BaseLinkedListTest {

    public static int[] reversePrint(ListNode head) {
        if (head == null) {
            return new int[]{};
        }
        if (head.next==null) {
            return new int[]{head.val};
        }
        Stack<ListNode> stack=new Stack<>();
        while (head!=null){
            stack.push(head);
            head=head.next;
        }
        int[] resp=new int[stack.size()];
        for (int i = 0; i < resp.length; i++) {
            resp[i]= stack.pop().val;
        }
        return resp;
    }

    public static void main(String[] args) {
        int length = 0;
        System.out.println(reversePrint(createLinkedList(1, 2, 3, 4, 5)));
    }

}
